import { _decorator, Component, instantiate, Node, Slider, Vec3 } from 'cc';
import { GAME_PREFAB, GlobalData, SOUND_TYPE } from '../../../Data/GlobalData';
import { SingleMajiang } from './SingleMajiang';
import { MajiangLoad } from '../MajiangLoad';
const { ccclass, property } = _decorator;

@ccclass('SingleLine')
export class SingleLine extends Component {

    //锁层
    @property(Node)
    public LockLoad: Node = null;

    //解锁按钮
    @property(Node)
    private LockBtn: Node = null;

    //麻将层
    @property(Node)
    public Majiang: Node = null;

    //点击的麻将
    private ClickMajiang: Node[] = [];

    //显示麻将
    public ShowMajiang: Node[] = [];

    //点击的索引
    public ClickIndex: number = -1;

    //麻将层
    public MajiangLoad: MajiangLoad = null;

    /**
     * 点击解锁按钮
     */
    private LockBtnClick() {
        app.audioManager.PlayEffect(SOUND_TYPE.Button);

        app.adManager.ShowVideo(0, (_isend: boolean) => {
            if (_isend) {
                this.LockLoad.active = false;
                app.topManager.ShowPrompt("解锁成功");
            } else {
                app.topManager.ShowPrompt("复活失败");
            }
        })


    }

    /**
     * 初始化
     */
    public Init(_num: number) {
        switch (_num) {
            case 0://解锁装填
                this.LockLoad.active = false;
                break;
            case 1://未解锁
                this.LockLoad.active = true;
                this.LockBtn.active = false;
                break;
            case 2://看视频解锁
                this.LockLoad.active = true;
                this.LockBtn.active = true;
                break;
        }

        for (let majiang of this.Majiang.children) majiang.active = false;

    }

    /**
     * 添加麻将
     */
    public AddMajiang(_worldpos: Vec3, _index: number) {
        for (let majiang of this.Majiang.children) {
            if (!majiang.active) {
                majiang.active = true;
                majiang.getComponent(SingleMajiang).UpdateMajiang(_index);
                majiang.getComponent(SingleMajiang).PlayMoveAni(_worldpos);
                break;
            }
        }

        //更新显示麻将数组
        this.ShowMajiang = [];

        for (let majiang of this.Majiang.children) {
            if (majiang.active) {
                this.ShowMajiang.push(majiang);
            }
        }

        //如果我满了我判断是否可消除
        if (this.ShowMajiang.length == this.Majiang.children.length) {

            let clear_index: number = this.ShowMajiang[0].getComponent(SingleMajiang).MajiangIndex;

            for (let i = 1; i < this.ShowMajiang.length; i++) {
                if (this.ShowMajiang[i].getComponent(SingleMajiang).MajiangIndex != clear_index) {
                    this.MajiangLoad.CheckGameOver();
                    return;
                }
            }

            //可消除
            app.topManager.NoTouchClick();
            app.audioManager.PlayEffect(SOUND_TYPE.Success);
            for (let i = this.ShowMajiang.length - 1; i >= 0; i--) {
                this.scheduleOnce(() => {
                    this.ShowMajiang[i].active = false;
                    app.audioManager.PlayEffect(SOUND_TYPE.Boom);
                    if (i == 0) {
                        this.ShowMajiang = [];
                        app.topManager.CanTouchClick();
                        this.MajiangLoad.UpdateMax(clear_index + 1, this.node)
                    }
                }, 0.1 * (this.ShowMajiang.length - i))
            }


        }

    }

    /**
     * 选择
     */
    public IamChoose() {
        //我最后一个麻将可以被点击的
        this.ClickIndex = this.ShowMajiang[this.ShowMajiang.length - 1].getComponent(SingleMajiang).MajiangIndex;

        for (let i = this.ShowMajiang.length - 1; i >= 0; i--) {
            if (this.ShowMajiang[i].getComponent(SingleMajiang).MajiangIndex == this.ClickIndex) {
                this.ClickMajiang.push(this.ShowMajiang[i]);
                this.ShowMajiang[i].getComponent(SingleMajiang).IamChoose();
            } else {
                break;
            }
        };

    }

    /**
     * 恢复
     */
    public IamReset() {
        for (let majiang of this.ClickMajiang) {
            majiang.getComponent(SingleMajiang).IamReset();
        }
        this.ClickMajiang = [];

        //更新显示麻将数组
        this.ShowMajiang = [];

        for (let majiang of this.Majiang.children) {
            if (majiang.active) {
                this.ShowMajiang.push(majiang);
            }
        }

    }

    /**
     * 错了
     */
    public IamWrong() {
        for (let majiang of this.ClickMajiang) {
            majiang.getComponent(SingleMajiang).ImDown();
        }
        this.ClickMajiang = [];
    }

    /**
     * 判断我是否可以被点击
     */
    public ReturnCanClick(): boolean {
        return this.Majiang.children[0].active;
    }

    /**
     * 返回最后的索引
     */
    public ReturnLastIndex(): number {
        if (this.ShowMajiang.length <= 0) return -1;
        return this.ShowMajiang[this.ShowMajiang.length - 1].getComponent(SingleMajiang).MajiangIndex;
    }

    /**
     * 添加点击麻将
     */
    public AddClickMajiang(_sl: SingleLine) {

        //我缺少多少个
        let add_num: number = this.Majiang.children.length - this.ShowMajiang.length;
        let add_majiang: number = _sl.ClickIndex;
        let add_majiangarr: Node[] = [];

        if (add_num > _sl.ClickMajiang.length) add_num = _sl.ClickMajiang.length;

        for (let i = 0; i < _sl.ClickMajiang.length; i++) {
            if (add_num <= 0) break;
            add_majiangarr.push(_sl.ClickMajiang[i])
            add_num--;
        }

        add_majiangarr = add_majiangarr.reverse();
        for (let i = 0; i < add_majiangarr.length; i++) {
            add_majiangarr[i].active = false;
            this.AddMajiang(add_majiangarr[i].getWorldPosition(), add_majiang)
        }

        _sl.IamReset();

    }

}


